<?php
/**
 * Enter point in application
 */

require_once BASE . '/library/Core/Exception.php';;
require_once BASE . '/library/Registry.php';;

class Application
{
    protected $_request = null;

    protected $_controller = null;

    protected $_layout = null;

    public function __construct()
    {   
        if( !is_readable( APP . "/Bootstrup.php" ) )
        {
            throw new Core_Exception( "No Bootstrup.php by path " . APP . "" );
        }
        
        require_once APP . "/Bootstrup.php";
    }

    public function run( array $configs = array() )
    {
        Registry::getInstance()->configs = $configs;

        $bootstrup = new Bootstrup();
        $bootstrup->setApplication( $this );
        $this->_runInitFunctions( $bootstrup );

        $this->_request = new Core_Request();
        $this->_request->initRequeset();
        Registry::getInstance()->request = $this->_request;

        $this->_initModule();
        $this->_initController();
        $this->_initAction();
   
    }

    public function getRequest()
    {
        return $this->_request;
    }

    public function _runInitFunctions( $obj )
    {
        $reflection = new ReflectionClass( $obj );
        $methods = $reflection->getMethods();

        foreach( $methods as $method )
        {
            $fn = $method->name;
            $sub = substr( $fn, 0, 5 );
            if( $sub == "_init" )
            {
                $obj->$fn();
            }
        }
    }

    public function setLayout( Core_Layout_Abstract $layout )
    {
        $this->_layout = $layout;
    }

    public function getLayout()
    {
        return $this->_layout;
    }

    private function _initModule()
    {
//        $configs = Registry::getInstance()->configs;
//        if( !isset( $configs['modules'] ) )
//        {
//            throw new Core_Exception("Модули не определены");
//        }
//
//        $modulePath = $this->_modules . $this->_request->getModule();
//        $moduleBootstrup = $this->_request->getModule() . "_Bootstrup";
//        $moduleBootstrupPath = APP . $modulePath . "/" . 'Bootstrup.php';
//
//        if( !is_file( $moduleBootstrupPath ) )
//        {
//            throw new Core_Exception( "Не определен загружчик модуля " . $this->_request->getModule() );
//        }
//
//        require_once $moduleBootstrupPath;
//
//        $moduleBootstrup = new $moduleBootstrup();
//        echo $modulePath; exit();
//        $moduleBootstrup->initAutoloadModule( "application" . $modulePath );
    }

    protected function _initController()
    {
        $this->_controller = $this->_request->getModule() . "_Controller_" . $this->_request->getController();
        $this->_controller = new $this->_controller;

        $this->_controller->setApplication( $this );
        $this->_controller->run();
    }

    protected function _initAction()
    {
        $action = $this->_request->getAction();
        $action[0] = strtolower( $action[0] );
        $action = $action . "Action";

        if( !method_exists($this->_controller, $action) )
        {
            throw new Core_Exception( "Action " . $action . " не оприделен" );
        }

        $this->_controller->$action();
    }



}

?>
